<?php
class Wishes extends Zend_Form
{
	public function init()
	{
		$this->setAction("/tender/wishes");
    	$this->setMethod("post");

    	$begin = new Zend_Session_Namespace('tender/begin');
    	$wishes = new Zend_Session_Namespace('tender/wishes');

    	$cid = array();
    	$countries = array();
    	if ( isset($wishes->country_off_id) ) $cid = array_merge($cid, $wishes->country_off_id);
    	if ( isset($wishes->country_on_id) ) $cid = array_merge($cid, $wishes->country_on_id);

    	if ( count($cid) ) {
    		$countries = Agitum_Model_Geo::fetchMapId($cid);
    	}

    	$elements = array();
 		$stars = new Zend_Form_Element_Select('stars', array('id' => 'stars'));
		$stars->setLabel('Категоря отдыха')
        	->addFilter('StringTrim')
        	->addFilter('StripTags')
        	->addValidator('Digits')
        	->setRequired(true);

        for($i = 2; $i < 6; $i++ ) {
        	$stars->addMultiOption($i, $i.' звезд'.($i < 5 ? 'ы' : ''));
        }

        if ( isset($wishes->stars) ) {
        	$stars->setValue($wishes->stars );
        } else {
        	$stars->setValue($begin->budget == 0 ? 5 : 3);
        }
        $elements[] = $stars;

        $visa = new Zend_Form_Element_Select('visa', array('id' => 'visa'));
		$visa->setLabel('Визы')
        	->addFilter('StringTrim')
        	->addFilter('StripTags')
        	->addValidator('Digits')
        	->setRequired(true);
        
        $slot = new Agitum_Model_Cache_Slot_Visa_List();
        $data = $slot->load();
        if ( !$data ) {
        	$obj = new Agitum_Model_Visa();
        	$data = $obj->fetchAll();
        	foreach ( $data as $i => $row ) {
        		$slot->addTag(new Agitum_Model_Cache_Tag_Visa($row['id']));
        	}
        	$slot->addTag(new Agitum_Model_Cache_Tag_Visa_List());
        	$slot->save($data);
        }
        foreach ( $data as $i => $row) {
        	$visa->addMultiOption($row['id'], $row['title']);
        }
        $visa->setValue( isset($wishes->visa) ? $wishes->visa : 0 );
		$elements[] = $visa;

        $slot = new Agitum_Model_Cache_Slot_Tour_List();
        $data = $slot->load();
        if ( !$data ) {
        	$tour = new Agitum_Model_Tour();
        	$data = $tour->fetchAll();
        	foreach ( $data as $i => $row ) {
        		$slot->addTag(new Agitum_Model_Cache_Tag_Tour($row['id']));
        	}
        	$slot->addTag(new Agitum_Model_Cache_Tag_Tour_List());
        	$slot->save($data);
        }

        $toff = new Zend_Form_Element_MultiCheckbox('tour_off', array('id' => 'tour-off'));
        $toff->setLabel('Тип тура <i>исключить</i>')
        	->addFilter('StringTrim')
        	->addFilter('StripTags')
        	->addValidator('Digits')
        	;

        shuffle($data);
        foreach ( $data as $i => $row ) {
        	$toff->addMultiOption($row['id'], $row['title']);
        }

        if ( isset($wishes->tour_off)) {
        	$toff->setValue($wishes->tour_off);
        }
        $elements[] = $toff;

        $country_off = new Zend_Form_Element_Radio('country_off', array('id' => 'country-off'));
        $country_off->setLabel('Страны <i>исключить</i>')
        	->addMultiOption(0,'Любые')
        	->addMultiOption(1,'Указать')
      		->setSeparator('');
      	$country_off->setValue( isset($wishes->country_off) ? $wishes->country_off : 'any');
      	$elements[] = $country_off;

      	$country_off_id = new Zend_Form_Element_MultiCheckbox("country_off_id", array('id' => "country-off-id"));
       	$country_off_id->setLabel('Выбранные страны')
        	->addFilter('StringTrim')
        	->addFilter('StripTags')
        	->addValidator('Digits')
        	->setSeparator('')
        	->setRegisterInArrayValidator(false)
        	;

        if ( isset($wishes->country_off_id) ) {
        	foreach($wishes->country_off_id as $i => $id) {
        		$country_off_id->addMultiOption($id, $countries[$id]['title']);
        	}
        	$country_off_id->setValue($wishes->country_off_id);
        }
        $elements[] = $country_off_id;

        $ton = new Zend_Form_Element_MultiCheckbox('tour_on', array('id' => 'tour-on'));
        $ton->setLabel('Тип тура <i>предложить</i>')
        	->addFilter('StringTrim')
        	->addFilter('StripTags')
        	->addValidator('Digits')
        	;

        shuffle($data);
        foreach ( $data as $i => $row ) {
        	$ton->addMultiOption($row['id'], $row['title']);
        }

		if ( isset($wishes->tour_on)) {
        	$ton->setValue($wishes->tour_on);
        }
        $elements[] = $ton;

      	$country_on = new Zend_Form_Element_Radio('country_on', array('id' => 'country-on'));
        $country_on->setLabel('Страны <i>предложить</i>')
        	->addMultiOption(0,'Любые')
        	->addMultiOption(1,'Указать')
      		->setSeparator('');
      	$country_on->setValue( isset($wishes->country_on) ? $wishes->country_on : 'any');
      	$elements[] = $country_on;

      	$country_on_id = new Zend_Form_Element_MultiCheckbox("country_on_id", array('id' => "country-on-id"));
       	$country_on_id->setLabel('Выбранные страны')
        	->addFilter('StringTrim')
        	->addFilter('StripTags')
        	->addValidator('Digits')
        	->setSeparator('')
        	->setRegisterInArrayValidator(false)
        	;
		if ( isset($wishes->country_on_id) ) {
        	foreach($wishes->country_on_id as $i => $id) {
        		$country_on_id->addMultiOption($id, $countries[$id]['title']);
        	}
        	$country_on_id->setValue($wishes->country_on_id);
        }
        $elements[] = $country_on_id;

      	$extra = new Zend_Form_Element_Textarea('extra', array('id'=>'extra', 'rows' => '5', 'cols' => '70'));
      	$extra->setLabel('Дополнительное описание')
      		->addFilter('StringTrim')
      		->addFilter('StripTags')
      		;

      	if ( isset($wishes->extra)) {
      		$extra->setValue($wishes->extra);
      	}
      	$elements[] = $extra;

      	$sbmt = new Zend_Form_Element_Submit('submit', array('id' => 'continue'));
        $sbmt->setLabel('Продолжить');
        $elements[] = $sbmt;

      	$this->addElements( $elements );

        $toff->setDecorators(
        	array(
	        	'ViewHelper',
        		'Errors',
        		array(array('data' => 'HtmlTag'), array('tag' => 'div')),
	        	array(array('label' => 'Label'), array('tag' => 'div', 'escape' => false)),
	        	array(array('row'=>'HtmlTag'), array('tag' => 'div', 'id' => 'tour-off-element')),
	        	array(array('inner'=>'HtmlTag'), array('tag' => 'div', 'openOnly' => 'true', 'id' => 'tour-country-off', 'style' => 'float: left', 'placement' => 'PREPEND')),
	        	array(array('outer'=>'HtmlTag'), array('tag' => 'div', 'openOnly' => 'true', 'id' => 'tour-country-wrapper', 'placement' => 'PREPEND')),
        	)
        );

        $country_off_id->setDecorators(
        	array(
	        	'ViewHelper',
        		'Errors',
        		array(array('data' => 'HtmlTag'), array('tag' => 'div', 'id' => 'country-off-id-data')),
	        	array(array('label' => 'Label'), array('tag' => 'div', 'escape' => false)),
	        	array(array('row'=>'HtmlTag'), array('tag' => 'div', 'id' => 'country-off-id-element')),
	        	array(array('inner'=>'HtmlTag'), array('tag' => 'div', 'closeOnly' => 'true', 'placement' => 'APPEND')),
        	)
        );

        $ton->setDecorators(
        	array(
	        	'ViewHelper',
        		'Errors',
        		array(array('data' => 'HtmlTag'), array('tag' => 'div')),
	        	array(array('label' => 'Label'), array('tag' => 'div', 'escape' => false)),
	        	array(array('row'=>'HtmlTag'), array('tag' => 'div', 'id' => 'tour-on-element')),
	        	array(array('inner'=>'HtmlTag'), array('tag' => 'div', 'openOnly' => 'true', 'id' => 'tour-country-on', 'style' => 'float: left', 'placement' => 'PREPEND')),
        	)
        );

        $country_on_id->setDecorators(
        	array(
	        	'ViewHelper',
        		'Errors',
        		array(array('data' => 'HtmlTag'), array('tag' => 'div', 'id' => 'country-on-id-data')),
	        	array(array('label' => 'Label'), array('tag' => 'div', 'escape' => false)),
	        	array(array('row'=>'HtmlTag'), array('tag' => 'div', 'id' => 'country-on-id-element')),
	        	array(array('inner'=>'HtmlTag'), array('tag' => 'div', 'closeOnly' => 'true', 'placement'=>'APPEND')),
	        	array(array('clear'=>'HtmlTag'), array('tag' => 'div', 'style' => 'clear: both', 'placement'=>'APPEND')),
	        	array(array('outer'=>'HtmlTag'), array('tag' => 'div', 'closeOnly' => 'true', 'placement' => 'APPEND')),
        	)
        );

        foreach ( array('stars', 'visa', 'extra', 'country_off', 'country_on') as $i => $name ) {
        	$element = $this->getElement($name);
        	$element->setDecorators(
        		array(
		        	'ViewHelper',
        			'Errors',
		        	array(array('data' => 'HtmlTag'), array('tag' => 'div', 'id' => $element->getId().'-data')),
		        	array(array('label' => 'Label'), array('tag' => 'div', 'escape' => false)),
		        	array(array('row' => 'HtmlTag'), array('tag' => 'div', 'id' => $element->getId().'-element', 'class' => $element->class )),
	        	)
        	);
        }

		$sbmt->setDecorators(array('ViewHelper', array('data'=>'HtmlTag', 'tag'=>'div')));

        $this->setDecorators(array(
		    'FormElements',
		    array('HtmlTag', array('tag' => 'div', 'id' => 'tender-wishes')),
		    'Form',
		));

	}
}